package io.github.talelin.latticy.controller.v1.ag;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.ag.jcunit.CreateOrUpdateJcUnitDTO;
import io.github.talelin.latticy.mapper.ag.JcUnitMapper;
import io.github.talelin.latticy.model.ag.JcUnitDO;
import io.github.talelin.latticy.service.ag.JcUnitService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

//单位信息表
@RestController
@RequestMapping("/v1/ag/unit")
public class JcUnitController {

    @Autowired
    private JcUnitService jcUnitService;
    @Autowired
    private JcUnitMapper mapper;

    @GetMapping("/{id}")
    public JcUnitDO getJcUnit(@PathVariable(value = "id") @Positive(message = "请输入正整数")int id){
        JcUnitDO jcUnit =jcUnitService.getById(id);
        if (jcUnit==null){
            throw new NotFoundException("未找到单位信息");
        }
        return jcUnit;
    }

    @GetMapping("/list")
    public List<JcUnitDO> getJcUnits(){
        List<JcUnitDO> list = mapper.selectList(null);
        return list;
    }


    @GetMapping("/page")
    public PageResponseVO<JcUnitDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{count}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page}") Integer page
    ) {
        Page<JcUnitDO> pager = new Page<>(page, count);
        IPage<JcUnitDO> paging =jcUnitService.getUnitByPage(pager);
        return PageUtil.build(paging);
    }

    @GetMapping("/search")
    public PageResponseVO<JcUnitDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{count}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page}") Integer page,
            @RequestParam(name = "keyword") String keyword
    ) {
        Page<JcUnitDO> pager = new Page<>(page, count);
        IPage<JcUnitDO> paging = jcUnitService.searchUnitByKeyword(pager, keyword);
        return PageUtil.build(paging);
    }

    @PostMapping("/create")
    public CreatedVO createdJcUnit(@RequestBody @Validated CreateOrUpdateJcUnitDTO validator){
        jcUnitService.createJcUnit(validator);
        return new CreatedVO("创建成功");
    }

    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除单位信息", module = "单位", mount = true)
    @GroupRequired
    public DeletedVO deletedJcUnit(@PathVariable(value = "id") @Positive(message = "请输入正整数")int id){
        JcUnitDO jcUnit = jcUnitService.getById(id);
        if (jcUnit == null){
            throw new NotFoundException("单位信息未找到",10022);
        }
        jcUnitService.deleteById(jcUnit.getId());
        return new DeletedVO(14);
    }

    @PutMapping("/{id}")
    public UpdatedVO updatedJcUnit(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                                   @RequestBody @Validated CreateOrUpdateJcUnitDTO validator){
        JcUnitDO jcUnit = jcUnitService.getById(id);
        if (jcUnit == null){
            throw new NotFoundException("单位信息未找到",10022);
        }
        jcUnitService.updateJcUnit(jcUnit,validator);
        return new UpdatedVO("更新成功");
    }
}
